package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.client.VodClient;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-05-31
 */

@Api(description="课程视频管理")
@RestController
@RequestMapping("/eduservice/video")
// @CrossOrigin
public class EduVideoController {

    @Autowired
    private EduVideoService eduVideoService;

    @Autowired
    private VodClient vodClient;

    @ApiOperation(value = "添加课程视频")
    @PostMapping("addVideo")
    private R addVideo(@RequestBody EduVideo eduVideo){
        eduVideoService.save(eduVideo);
        return R.ok();
    }

    @ApiOperation(value = "课程视频详情")
    @GetMapping("getVideoInfo/{id}")
    private R getVideoInfo(@PathVariable String id){
        EduVideo eduVideo = eduVideoService.getById(id);
        return R.ok().data("eduVideo", eduVideo);
    }

    // 删除小节
    @ApiOperation(value = "删除小节")
    @DeleteMapping("{id}")
    private R deleteVideo(@PathVariable String id){

        EduVideo eduVideo = eduVideoService.getById(id);
        String videoSourceId = eduVideo.getVideoSourceId();
        if (!StringUtils.isEmpty(videoSourceId)) {
            R result = vodClient.removeVideo(videoSourceId);
            if (result.getCode() == 20001){
                return result;
//                throw new GuliException(20001, "删除视频失败，熔断器...");
            }
        }
        eduVideoService.removeById(id);
        return R.ok();
    }

    @ApiOperation(value = "修改课程视频")
    @PostMapping("updateVideo")
    private R updateVideo(@RequestBody EduVideo eduVideo){
        eduVideoService.updateById(eduVideo);
        return R.ok();
    }
}

